package com.nutiteq.mapper.forms;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.nutiteq.MapComponent;
import com.nutiteq.mapper.CustomZoomIndicator;
import com.nutiteq.mapper.MapScreen;
import com.nutiteq.mapper.Mapper;
import com.nutiteq.ui.DefaultZoomIndicator;

public class ChangeZoomIndicator extends List implements CommandListener {
  public static final Command CHANGE_ZOOM_INDICATOR = new Command("Change zoom indicator",
      Command.ITEM, 0);
  private final Command back;
  private final Command select;
  private final MapComponent mapDisplay;

  public ChangeZoomIndicator(final MapComponent mapDisplay) {
    super("Zoom indicator", List.IMPLICIT);
    this.mapDisplay = mapDisplay;

    back = new Command("Back", Command.BACK, 0);
    select = new Command("Select", Command.OK, 0);

    append("Default indicator", null);
    append("Custom indicator", null);

    addCommand(back);
    addCommand(select);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      Display.getDisplay(Mapper.instance).setCurrent(MapScreen.instance);
    } else if (cmd == select) {
      final int index = getSelectedIndex();
      if (index == 0) {
        mapDisplay.setZoomLevelIndicator(new DefaultZoomIndicator(0, 1));
      } else {
        final CustomZoomIndicator customZoomIndicator = new CustomZoomIndicator();
        mapDisplay.setZoomLevelIndicator(customZoomIndicator);
        customZoomIndicator.setCurrentZoom(mapDisplay.getZoom());
      }
      Display.getDisplay(Mapper.instance).setCurrent(MapScreen.instance);
    }
  }
}
